// 参考地址：https://www.cnblogs.com/shen55/p/11077677.html
// H5跳转小程序参考：https://blog.csdn.net/github_38281308/article/details/107310719
var jweixin = require('jweixin-module');
import {
	Base64
} from './libs/base64.js';

export default {
	//判断是否在微信中
	isWechat: function() {
		var ua = window.navigator.userAgent.toLowerCase();
		if (ua.match(/micromessenger/i) == 'micromessenger') {
			// console.log('是微信客户端')
			return true;
		} else {
			// console.log('不是微信客户端')
			return false;
		}
	},
	//初始化sdk配置
	initJssdk: function(callback) {
		let url = window.location.href.split('#')[0]
		console.log("configMiniProgramH5======", url)
		const href = Base64.encode(url);
		uniCloud.callFunction({
			name: "api-request",
			data: {
				action: 'getWeChatConfig',
				href: href
			}
		}).then(res => {
			if (res.result) {
				jweixin.config({
					debug: false,
					appId: res.result.appid,
					timestamp: res.result.timestamp,
					nonceStr: res.result.nonceStr,
					signature: res.result.signature,
					jsApiList: ['checkJsApi',
						'onMenuShareTimeline',
						'onMenuShareAppMessage',
						'getLocation',
						'showMenuItems'
					],
					openTagList: ['wx-open-launch-weapp']
				});
				//配置完成后，再执行分享等功能
				if (callback) {
					callback(res.result);
				}
			}
		})
	},
	//在需要自定义分享的页面中调用
	share: function(data) {
		if (!this.isWechat()) {
			console.log('不是微信客户端')
			return;
		}
		let url = window.location.href.split('#')[0]
		//每次都需要重新初始化配置，才可以进行分享
		this.initJssdk(function(signData) {
			jweixin.ready(function() {
				var shareData = {
					title: "推荐给你一个应用",
					desc: "我发现一个很好的应用，推荐给你看看。",
					link: url,
					imgUrl: "https://img-cdn-aliyun.dcloud.net.cn/stream/icon/__UNI__9AC5BB8.png",
					success: function(res) {
						//用户点击分享后的回调，这里可以进行统计，例如分享送金币之类的
						// post('/api/member/share');
					},
					cancel: function(res) {}
				};
				//分享给朋友接口
				jweixin.onMenuShareAppMessage(shareData);
				//分享到朋友圈接口
				jweixin.onMenuShareTimeline(shareData);
				// 显示弹窗
				jweixin.showMenuItems({
					menuList: ["menuItem:share:appMessage", "menuItem:share:timeline",
						"menuItem:share:qq", "menuItem:favorite"
					],
					success: function(res) {
						console.log(res);
						// callback(res)
					},
				});
				// callback()
			});
		});
	},
	//在需要定位页面调用
	location: function(callback) {
		if (!this.isWechat()) {
			console.log('不是微信客户端')
			return;
		}
		console.info('定位')
		this.initJssdk(function(res) {
			jweixin.ready(function() {
				console.info('定位ready=============')
				jweixin.getLocation({
					type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'
					success: function(res) {
						// console.log(res);
						callback(res)
					},
					fail: function(res) {
						console.log(res)
					}
				});
			});
		});
	},
	miniprogram: function(callback) {
		if (!this.isWechat()) {
			console.log('不是微信客户端')
			return;
		}
		console.info('小程序')
		this.initJssdk(function(res) {
			jweixin.ready(function() {
				console.info('小程序ready')
			});
		});
	}
}
